var express = require('express');
var router = express.Router();
var session = require('express-session');
var moment = require('moment');
var async = require('async');

var db = require('./../../utils/db.js');

router.post('/wordTree', function (req, res, next) {
    var parentId = req.body.parentId;
    var sql = '';
    if (parentId && parseInt(parentId) !== 0) {
        sql = "SELECT parent.id AS id, parent.type_name AS name,parent.code as code,  parent.parent_id AS pId, (SELECT COUNT(child.id) FROM daily_word_type AS child WHERE child.parent_id=parent.id) AS isParent FROM daily_word_type AS parent WHERE parent.parent_id = " + parentId + " ORDER BY parent.type_name ASC";
    } else {
        sql = "select parent.id AS id, parent.type_name AS name,parent.code as code, parent.parent_id AS pId, (SELECT COUNT(child.id) FROM daily_word_type AS child WHERE child.parent_id=parent.id) AS isParent from daily_word_type AS parent where depth=0 ORDER BY type_name ASC";
    }

    db.query(sql, function (err, rows) {
        if (rows && rows.length >= 0) {
            res.json(rows);

        } else {
            res.json({
                errcode: 1,
                message: '获取文档分类树失败'
            });
        }

    });
});


/**
 * 保存
 */
router.post('/save', function (req, res, next) {
    var typeName = req.body.typeName ? req.body.typeName.trim() : '';
    var parentId = req.body.parentId ? req.body.parentId.trim() : '';
    var depth = req.body.depth ? req.body.depth.trim() : '';

    var id = req.body.id;

    if (id) {
        var sql = "update daily_word_type set type_name='" + typeName + "',parent_id=" + parentId + " where id=" + id;
        db.query(sql, function (err, rows) {
            if (!err) {
                res.json({
                    errcode: 0,
                    message: '保存成功',
                    data:rows
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '保存失败'
                });
            }
        });
    } else {
        async.waterfall([
            function(callback){
                var selectTypeSql = "select daily_word_type.code as typeCode,\n" +
                    "(select count(*) from daily_word_type as cdwt where cdwt.parent_id = daily_word_type.id) as childrenNumher\n" +
                    "from daily_word_type where id = '"  + parentId + "'";
                db.query(selectTypeSql, function (err, rows) {
                    if (!err && rows[0]){
                        var data  = rows[0];
                        if (data.typeCode === '0'){
                            var newCode = "000" + (data.childrenNumher + 1);
                            callback(err, newCode);
                        }else {
                            var newCode = data.typeCode + "000" + (data.childrenNumher + 1);
                            callback(err, newCode);
                        }
                    }
                });
            }
        ], function (err, result) {
            if (result){
                var  sql = "insert into daily_word_type(type_name,parent_id,depth,create_time,create_by_id,remark,code) values ('" + typeName + "'," + parentId + "," + depth + ",'2018-03-06 15:57:45',1,'','" + result + "')";
                db.query(sql, function (errsql, data) {
                    if (!errsql) {
                        var selectTypeSql = "select id,code from daily_word_type where id = '" + data.insertId + "'";
                        db.query(selectTypeSql, function (errTypeSql, rows) {
                            if (!errTypeSql) {
                                res.json({
                                    errcode: 0,
                                    message: '保存成功',
                                    data:rows[0]
                                });
                            }else {
                                res.json({
                                    errcode: 1,
                                    message: '保存失败'
                                });
                            }
                        });

                    } else {
                        res.json({
                            errcode: 1,
                            message: '保存失败'
                        });
                    }
                });
            }else {
                res.json({
                    errcode: 1,
                    message: '保存失败'
                });
            }

        });

    }



});

/**
 * 删除
 */
router.post('/delete/:id', function (req, res, next) {
    var id = req.params.id;
    var sql = '';
    if (id) {
        sql = "delete from daily_word_type where id=" + id;
        db.query(sql, function (err, rows) {
            if (!err) {
                res.json({
                    errcode: 0,
                    message: '删除成功'
                });
            } else {
                res.json({
                    errcode: 1,
                    message: '删除失败'
                });
            }
        });
    } else {
        res.json({
            errcode: 2,
            message: '系统错误'
        });
    }


});



module.exports = router;